<?php
/**
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com.cn
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */

namespace addon\city\city\controller;

use app\model\web\DiyView as DiyViewModel;

/**
 * 网站装修控制器
 */
class Diy extends BaseCity
{
	private $page = "DIYVIEW_INDEX";

	/**
	 * 网站主页
	 */
	public function index()
	{

		// 查询公共组件和支持的页面
		$condition = [
			[ 'support_diy_view', 'like', [ $this->page, '%' . $this->page . ',%', '%' . $this->page, '%,' . $this->page . ',%', '' ], 'or' ]
		];
		$this->assign("name", $this->page);
		$this->editData($condition, 0, $this->page);
		return $this->fetch('diy/edit', [], $this->replace);
	}

	/**
	 * 编辑
	 */
	public function edit()
	{
		if (request()->isAjax()) {
			$res = 0;
			$data = array ();
			$id = input("id", 0);
			$name = input("name", "");
			$title = input("title", "");
			$value = input("value", "");
			if (!empty($name) && !empty($title) && !empty($value)) {
				$diy_view = new DiyViewModel();
				$data[ 'site_id' ] = $this->site_id;
				$data[ 'name' ] = $name;
				$data[ 'title' ] = $title;
				$data[ 'type' ] = 'city';
				$data[ 'value' ] = $value;
				if ($id == 0) {
					$data[ 'create_time' ] = time();
					$res = $diy_view->addSiteDiyView($data);
				} else {
					$data[ 'update_time' ] = time();
					$res = $diy_view->editSiteDiyView($data, [
						[ 'id', '=', $id ]
					]);
				}
			}

			return $res;
		} else {

			$id = input("id", 0);
			//查询公共系统组件
			$condition = [
				[ 'support_diy_view', '=', '' ]
			];
			$this->editData($condition, $id);
			return $this->fetch('diy/edit', [], $this->replace);
		}
	}

	/**
	 * 编辑时用到的数据
	 * @param $condition
	 * @param $id
	 * @param string $name
	 */
	private function editData($condition, $id, $name = '')
	{
		$diy_view = new DiyViewModel();

		// 自定义模板组件集合
		$utils = $diy_view->getDiyViewUtilList($condition);

		$diy_view_info = [];
		// 推广码
		$qrcode_info = [];
		if (!empty($id)) {
			$diy_view_info = $diy_view->getSiteDiyViewDetail([
				[ 'sdv.site_id', '=', $this->site_id ],
				[ 'sdv.id', '=', $id ]
			]);
			$qrcode_info = $diy_view->qrcode([
				[ 'site_id', '=', $this->site_id ],
				[ 'id', '=', $id ]
			]);
		} elseif (!empty($name)) {
			$condition = [
				[ 'sdv.site_id', '=', $this->site_id ],
				[ 'sdv.name', '=', $name ]
			];
			$qrcode_info = $diy_view->qrcode([
				[ 'site_id', '=', $this->site_id ],
				[ 'name', '=', $name ]
			]);
			$diy_view_info = $diy_view->getSiteDiyViewDetail($condition);
		}

		if (!empty($diy_view_info) && !empty($diy_view_info[ 'data' ])) {
			$diy_view_info = $diy_view_info[ 'data' ];
		}

		if (!empty($qrcode_info)) {
			$qrcode_info = $qrcode_info[ 'data' ];
			// 目前只支持H5
			if ($qrcode_info[ 'path' ][ 'h5' ][ 'status' ] != 1) {
				$qrcode_info = [];
			}
		}

		$diy_view_utils = array ();
		if (!empty($utils[ 'data' ])) {

			// 先遍历，组件分类
			foreach ($utils[ 'data' ] as $k => $v) {
				$value = array ();
				$value[ 'type' ] = $v[ 'type' ];
				$value[ 'type_name' ] = $diy_view->getTypeName($v[ 'type' ]);
				$value[ 'list' ] = [];
				if (!in_array($value, $diy_view_utils)) {
					array_push($diy_view_utils, $value);
				}
			}

			// 遍历每一个组件，将其添加到对应的分类中
			foreach ($utils[ 'data' ] as $k => $v) {
				foreach ($diy_view_utils as $diy_k => $diy_v) {
					if ($diy_v[ 'type' ] == $v[ 'type' ]) {
						array_push($diy_view_utils[ $diy_k ][ 'list' ], $v);
					}
				}
			}
		}

		$this->assign("time", time());
		$this->assign("qrcode_info", $qrcode_info);
		$this->assign('diy_view_utils', $diy_view_utils);
		$this->assign("diy_view_info", $diy_view_info);
	}

	/**
	 * 微页面
	 */
	public function lists()
	{
		if (request()->isAjax()) {
			$page_index = input('page', 1);
			$page_size = input('page_size', PAGE_LIST_ROWS);
			$diy_view = new DiyViewModel();
			$condition = array (
				[ 'sdv.site_id', '=', $this->site_id ],
				[ 'sdv.type', '=', 'city' ],
				[ 'sdv.name', 'like', '%DIY_VIEW_RANDOM_%' ]
			);
			$list = $diy_view->getSiteDiyViewPageList($condition, $page_index, $page_size, "sdv.create_time desc");
			return $list;
		} else {
			return $this->fetch('diy/lists', [], $this->replace);
		}
	}

	/**
	 * 链接选择
	 */
	public function link()
	{
		$link = input("link", []);
		$support_diy_view = input("support_diy_view", '');//支持的自定义页面（为空表示都支持）
		$is_array = true;//记录是否是数组，后续判断受该变量影响
		if (!empty($link)) {
			$link = json_decode($link, true);
			if (empty($link[ 'name' ])) {
				$link = [];
			}
			$is_array = is_array($link);
		}

		$diy_view_model = new DiyViewModel();
		$link_condition = [
			[ 'lk.support_diy_view', 'like', [ $support_diy_view, '' ], 'or' ]
		];
		$res = $diy_view_model->getDiyLinkList($link_condition);
		$res = $res[ 'data' ];

		//查询当前站点的微页面，暂时不显示
//		$page = $diy_view_model->getPage();
//		$condition = array(
//			'sdv.site_id' => $this->site_id,
//			'sdv.type' => $page['admin']['port'],
//			'ndva.addon_name' => null, //排除插件中的自定义模板
//		);

//		$site_diy_view_list = $diy_view_model->getSiteDiyViewPageList($condition, 1, 0, "sdv.create_time desc");
//		$site_diy_view_list = $site_diy_view_list['data']['list'];

//		$addon_model = new Addon();
		$link_list = array ();

		// 调整链接结构，先遍历，插件分类
		foreach ($res as $k => $v) {
			$value = array ();
			if (empty($v[ 'addon_name' ])) {
				$v[ 'addon_title' ] = '系统';
			}
			$value[ 'addon_name' ] = $v[ 'addon_name' ];
			$value[ 'addon_title' ] = $v[ 'addon_title' ];
			$value[ 'icon' ] = $v[ 'addon_icon' ];
			$value[ 'list' ] = [];
			$column = array_column($link_list, 'addon_name');
			if (!in_array($v[ 'addon_name' ], $column)) {
				array_push($link_list, $value);
			}
		}

//		foreach ($site_diy_view_list as $k => $v) {
//			$value = array();
//			$addon_info = $addon_model->getAddonInfo([ 'name' => $v['addon_name'] ], 'title,icon');
//			$addon_info = $addon_info ['data'];
//			$value['addon_name'] = $v['addon_name'];
//			$value['addon_title'] = $addon_info['title'];
//			$value['icon'] = $addon_info['icon'];
//			$value['list'] = [];
//			$column = array_column($link_list, 'addon_name');
//			if (!in_array($v['addon_name'], $column)) {
//				array_push($link_list, $value);
//			}
//		}

		$temp_link = [];

		// 遍历每一个链接，将其添加到对应的插件分类中
		foreach ($link_list as $diy_k => $diy_v) {

			//遍历插件固定自定义页面
			foreach ($res as $fixed_k => $fixed_v) {
				if ($diy_v[ 'addon_name' ] == $fixed_v[ 'addon_name' ]) {
					if (!empty($link) && $is_array && $link[ 'name' ] == $fixed_v[ 'name' ]) {
						//对象方式匹配
						$fixed_v[ 'selected' ] = true;
					} elseif (!empty($link) && !$is_array && strtolower($link) == strtolower($fixed_v[ 'wap_url' ])) {
						//字符串方式匹配
						$fixed_v[ 'selected' ] = true;
						$temp_link = $fixed_v;
					} else {
						$fixed_v[ 'selected' ] = false;
					}
					array_push($link_list[ $diy_k ][ 'list' ], $fixed_v);
				}
			}

			//遍历微页面
//			foreach ($site_diy_view_list as $page_k => $page_v) {
//
//				if ($diy_v['addon_name'] == $page_v['addon_name']) {
//
//					$item = [
//						'id' => $page_v['id'],
//						'name' => $page_v['name'],
//						'title' => $page_v['title'],
//						'addon_icon' => "",
//						'addon_name' => $page_v['addon_name'],
//						'addon_title' => $diy_v['addon_title'],
//						'web_url' => '',
//						'wap_url' => '/otherpages/diy/diy/diy?name=' . $page_v['name'],
//						'icon' => '',
//						'type' => 0
//					];
//
//					if (!empty($link) && $is_array && $link['name'] == $page_v['name']) {
//						//对象方式匹配
//						$item['selected'] = true;
//					} elseif (!empty($link) && !$is_array && strtolower($link) == strtolower($page_v['wap_url'])) {
//						//字符串方式匹配
//						$item['selected'] = true;
//						$temp_link = $page_v;
//					} else {
//						$item['selected'] = false;
//					}
//					array_push($link_list[ $diy_k ]['list'], $item);
//
//				}
//			}
		}

		if (!$is_array) {
			$link = $temp_link;
		}

		$this->assign("link", $link);
		$this->assign("link_list", $link_list);
		return $this->fetch('diy/link', [], $this->replace);
	}

	/**
	 * 删除自定义模板页面
	 */
	public function deleteSiteDiyView()
	{
		if (request()->isAjax()) {
			$diy_view = new DiyViewModel();
			$id_array = input("id", 0);
			$condition = [
				[ 'id', 'in', $id_array ]
			];
			$res = $diy_view->deleteSiteDiyView($condition);
			return $res;
		}
	}

	/**
	 * 推广链接
	 */
	public function promote()
	{
		if (request()->isAjax()) {
			$id = input("id", 0);
			$diy_view = new DiyViewModel();
			$res = $diy_view->qrcode([
				[ 'site_id', '=', $this->site_id ],
				[ 'id', '=', $id ]
			]);
			return $res;
		}
	}
}